/*
 * Copyright (c) 2009 LastFM.Desktop Anton Pavlik.
 * All Rights Reserved.
 * 
 * You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, 
 * with or without modifications, and in Source or Object form, provided that You meet the following 
 * conditions:
 * 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
 *
 * 2. You must cause any modified files to carry prominent notices stating that You changed the files; 
 * and
 *
 * 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, 
 * patent, trademark, and attribution notices from the Source form of the Work, excluding those notices 
 * that do not pertain to any part of the Derivative Works.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, Licensor provides the Work 
 * (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties 
 * or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. 
 * YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE WORK
 * AND ASSUME ANY RISKS ASSOCIATED WITH YOUR EXERCISE OF PERMISSIONS UNDER THIS LICENSE. 
 */
package org.shake.lastfm.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlrpc.XmlRpcException;
import org.shake.lastfm.api.method.MethodExecutor;
import org.shake.lastfm.api.method.XmlRpcMethodException;
import org.shake.lastfm.api.method.XmlRpcMethodName;
import org.shake.lastfm.api.method.XmlRpcMethodParamName;
import org.shake.lastfm.model.Album;
import org.shake.lastfm.model.Artist;
import org.shake.lastfm.model.GeoEvent;
import org.shake.lastfm.model.Picture;
import org.shake.lastfm.model.TopTrack;

public class XmlRpcApiImpl implements XmlRpcApi
{
    private static final String API_KEY = "49b8330a1c97b5116287f1b52b765880";
    
    private MethodExecutor executor;

    private Log log = LogFactory.getLog(XmlRpcApi.class);
    
    public XmlRpcApiImpl()
    {
        this.executor = new MethodExecutor();
    }

    @Override
    public Album fetchAlbumInfo(String artistName, String albumName) throws XmlRpcException
    {
        Map<XmlRpcMethodParamName, String> params = new HashMap<XmlRpcMethodParamName, String>();
        params.put(XmlRpcMethodParamName.ARTIST, artistName);
        params.put(XmlRpcMethodParamName.ALBUM, albumName);
        params.put(XmlRpcMethodParamName.API_KEY, API_KEY);

        
        Album result = null;
        
        try {
            result = this.executor.executeMethod(XmlRpcMethodName.ALBUM_GETINFO, params);
            log.info("Executed " + XmlRpcMethodName.ALBUM_GETINFO.getName() + "(\"" + artistName +"\",\""+albumName+" \")");
        } catch (XmlRpcMethodException ex)
        {
            log.error(ex);
        }
        
        return result;
    }

    @Override
    public Artist fetchArtistInfo(String artistName) throws XmlRpcException
    {
        Map<XmlRpcMethodParamName, String> params = new HashMap<XmlRpcMethodParamName, String>();
        params.put(XmlRpcMethodParamName.ARTIST, artistName);
        params.put(XmlRpcMethodParamName.API_KEY, API_KEY);
        
        Artist result = null;
        try
        {
            result = this.executor.executeMethod(XmlRpcMethodName.ARTIST_GETINFO, params);
            log.info("Executed " + XmlRpcMethodName.ARTIST_GETINFO.getName() + "(\"" + artistName +"\")");
        } catch (XmlRpcMethodException ex)
        {
            log.error(ex);
        }
        
        return result;
    }

    @Override
    public List<Picture> fetchArtistPictures(String artistName) throws XmlRpcException
    {
        Map<XmlRpcMethodParamName, String> params = new HashMap<XmlRpcMethodParamName, String>();
        params.put(XmlRpcMethodParamName.ARTIST, artistName);
        params.put(XmlRpcMethodParamName.API_KEY, API_KEY);
        
        List<Picture> result = null;
        
        try
        {
            result = this.executor.executeMethod(XmlRpcMethodName.ARTIST_GETIMAGES, params);
            log.info("Executed " + XmlRpcMethodName.ARTIST_GETIMAGES.getName() + "(\"" + artistName +"\")");
        } catch (XmlRpcMethodException ex)
        {
            log.error(ex);
        }
        
        return result;
    }
    
	@Override
	public List<TopTrack> fetchTopTracks(String userName, String period) throws XmlRpcException {
		Map<XmlRpcMethodParamName, String> params = new HashMap<XmlRpcMethodParamName, String>();
		params.put(XmlRpcMethodParamName.USER, userName);
		params.put(XmlRpcMethodParamName.PERIOD, period);
		params.put(XmlRpcMethodParamName.API_KEY, API_KEY);
		
		List<TopTrack> result = null;
		
		try
		{
			result = this.executor.executeMethod(XmlRpcMethodName.USER_GETTOPTRACKS, params);
			log.info("Executed " + XmlRpcMethodName.USER_GETTOPTRACKS.getName() + "(\"" + userName +"\", \""+period+"\")");
		} catch (XmlRpcMethodException ex)
		{
			log.error(ex);
		}
				
		return result;
				
	}
	
	@Override
	public List<GeoEvent> fetchGeoEvents(String location) throws XmlRpcException 
	{
		return fetchGeoEvents(location, 0, 1);
	}
	
	@Override
	public List<GeoEvent> fetchGeoEvents(String location, Integer distance, Integer page) throws XmlRpcException 
	{
		Map<XmlRpcMethodParamName, String> params = new HashMap<XmlRpcMethodParamName, String>();
		params.put(XmlRpcMethodParamName.LOCATION, location);
		params.put(XmlRpcMethodParamName.DISTANCE, String.valueOf(distance));
		params.put(XmlRpcMethodParamName.PAGE, String.valueOf(page));
		params.put(XmlRpcMethodParamName.API_KEY, API_KEY);
		
		List<GeoEvent> result = null;
		
		try
		{
			result = this.executor.executeMethod(XmlRpcMethodName.GEO_GETEVENTS, params);
			log.info("Executed " + XmlRpcMethodName.GEO_GETEVENTS.getName() + "(\"" + location +"\")");
		} catch (XmlRpcMethodException ex)
		{
			log.error(ex);
		}
					
		return result;
		
	}
	
}